/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
define(["require", "exports", "../protocol.const", "./baseCodeLensProvider", "../utils/convert"], function (require, exports, PConst, baseCodeLensProvider_1, convert_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    class TypeScriptReferencesCodeLensProvider extends baseCodeLensProvider_1.TypeScriptBaseCodeLensProvider {
        constructor(client, language, cachedResponse, vscode) {
            super(client, cachedResponse, vscode);
            this.language = language;
        }
        updateConfiguration() {
            const config = this.vscode.workspace.getConfiguration(this.language);
            this.setEnabled(config.get('referencesCodeLens.enabled', false));
        }
        provideCodeLenses(document, token) {
            const _super = name => super[name];
            return __awaiter(this, void 0, void 0, function* () {
                if (!this.client.apiVersion.has206Features()) {
                    return [];
                }
                return _super("provideCodeLenses").call(this, document, token);
            });
        }
        resolveCodeLens(inputCodeLens, token) {
            const codeLens = inputCodeLens;
            const args = convert_1.vsPositionToTsFileLocation(codeLens.file, codeLens.range.start);
            return this.client.execute('references', args, token).then(response => {
                if (!response || !response.body) {
                    throw codeLens;
                }
                const locations = response.body.refs
                    .map((reference) => new this.vscode.Location(this.client.asUrl(reference.file), convert_1.tsTextSpanToVsRange(reference, this.vscode)))
                    .filter((location) => 
                // Exclude original definition from references
                !(location.uri.toString() === codeLens.document.toString() &&
                    location.range.start.isEqual(codeLens.range.start)));
                codeLens.command = {
                    title: locations.length === 1
                        ? '1 引用'
                        : locations.length + ' 引用',
                    command: locations.length ? 'editor.action.showReferences' : '',
                    arguments: [codeLens.document, codeLens.range.start, locations]
                };
                return codeLens;
            }).catch(() => {
                codeLens.command = {
                    title: 'Could not determine references',
                    command: ''
                };
                return codeLens;
            });
        }
        extractSymbol(document, item, parent) {
            if (parent && parent.kind === PConst.Kind.enum) {
                return super.getSymbolRange(document, item);
            }
            switch (item.kind) {
                case PConst.Kind.const:
                case PConst.Kind.let:
                case PConst.Kind.variable:
                case PConst.Kind.function:
                    // Only show references for exported variables
                    if (!item.kindModifiers.match(/\bexport\b/)) {
                        break;
                    }
                // fallthrough
                case PConst.Kind.class:
                    if (item.text === '<class>') {
                        break;
                    }
                // fallthrough
                case PConst.Kind.memberFunction:
                case PConst.Kind.memberVariable:
                case PConst.Kind.memberGetAccessor:
                case PConst.Kind.memberSetAccessor:
                case PConst.Kind.constructorImplementation:
                case PConst.Kind.interface:
                case PConst.Kind.type:
                case PConst.Kind.enum:
                    return super.getSymbolRange(document, item);
            }
            return null;
        }
    }
    exports.default = TypeScriptReferencesCodeLensProvider;
});
